
import { defineStore } from 'pinia'

interface SelectState {
  cache: any
}
export const useSelectStore = defineStore('app-select', {
  // 推荐使用 完整类型推断的箭头函数
  state: (): SelectState => {
    return {
      cache: {}
    }
  },
  actions: {
    /**
     * 请求数据
     * @param key 
     * @param api 
     * @param params
     * @param fun 原有的requestData函数
     */
    requestData(key: any,api:any,params: any,fun: (api:any,params: any) => Promise<any>){
      if(this.cache[key] || !fun) return;
      this.cache[key] = []
      fun(api,params).then((res)=>{
        this.cache[key] = res.data
      }).catch(()=>{
        delete this.cache[key]
      })
    }
  },
  getters: {
    /**
     * 获取数据
     * @returns 
     */
   getSelectData(): Function {
     return (key:any)=>{
      return this.cache[key]
     }
   }
  }
})